Skip to content

feat: add email management tools (mark read, move, search, list mailboxes)#116

Open
rhubain wants to merge 6 commits intoai-zerolab:mainfrom
rhubain:feature/mark-read-and-move
Open

feat: add email management tools (mark read, move, search, list mailboxes)#116
rhubain wants to merge 6 commits intoai-zerolab:mainfrom
rhubain:feature/mark-read-and-move

Conversation

@rhubain
Copy link

@rhubain rhubain commented Feb 8, 2026

Summary

This PR adds several new MCP tools for comprehensive email management:

  • list_mailboxes - Discover available folders (Archive, Sent, Trash, etc.)
  • search_emails - Server-side text search using IMAP SEARCH (fast even with thousands of emails)
  • mark_emails_as_read - Mark emails as read/unread
  • move_emails - Move emails between folders (uses IMAP MOVE with COPY+DELETE fallback)

Also includes:

  • Performance fix: Optimized pagination to avoid fetching all dates (O(n) → O(page_size))
  • Bug fix: Fixed IMAP SEARCH response parsing that incorrectly treated status messages as UIDs

New Tools

Tool Description
list_mailboxes List all folders in the account
search_emails Search by text in subject/body/from/all
mark_emails_as_read Set or clear the \Seen flag
move_emails Move to Archive, Trash, or any folder

Test plan

  • Tested manually with iCloud account (9400+ archived emails)
  • list_mailboxes returns correct folder list
  • list_emails_metadata with large mailbox completes in <5s (was 60s+)
  • search_emails finds emails by keyword
  • move_emails successfully moves between folders
  • All 136 unit tests pass
  • make check passes (linting + formatting)

Technical notes

  • Uses UID ordering for pagination (UIDs are ascending by date added to mailbox per RFC 3501)
  • IMAP MOVE command (RFC 6851) with COPY+DELETE fallback for servers without MOVE support
  • Server-side search avoids downloading all emails for filtering

⚠️ Fair warning: this was vibecoded on a Sunday night around midnight. Tests pass and it works on my machine™, but a careful review would be much appreciated! 😅

🤖 Generated with Claude Code

Anonymous and others added 6 commits February 8, 2026 22:57
Add two new MCP tools for email management:
- mark_emails_as_read: Mark emails as read/unread using IMAP \Seen flag
- move_emails: Move emails between mailboxes using MOVE (RFC 6851)
  with fallback to COPY+DELETE for older servers

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Fix bug where status messages like "SEARCH completed (took 5 ms)"
were incorrectly parsed as email UIDs. The number in the timing
info (e.g., "5") was being treated as a valid UID.

Add _parse_search_response() method that:
- Detects status messages by checking for keywords
- Returns empty list for status-only responses
- Only returns actual numeric UIDs from valid responses

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add new MCP tool to list all mailboxes (folders) in an email account.
Returns mailbox name, flags, and hierarchy delimiter.

Useful for discovering folder names like Archive, Sent, Trash which
may vary across email providers (e.g., iCloud uses different names).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Performance optimization:
- Don't fetch INTERNALDATE for all emails when paginating
- Use UID ordering directly (UIDs are ascending by add date)
- Only fetch headers for the requested page

New search_emails tool:
- Server-side IMAP SEARCH (fast even with thousands of emails)
- Search in: all (TEXT), subject, body, or from
- Paginated results

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Use ternary operator in _parse_search_response (SIM108)
- Replace raise Exception with logging + failed_ids (TRY301)
- Add tests for list_mailboxes, search_emails, mark_emails_as_read, move_emails
- Update test_get_emails_stream to match optimized pagination behavior

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@codecov
Copy link

codecov bot commented Feb 8, 2026

Codecov Report

❌ Patch coverage is 25.38071% with 147 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
mcp_email_server/emails/classic.py 20.7% 125 Missing and 5 partials ⚠️
mcp_email_server/app.py 32.0% 17 Missing ⚠️

📢 Thoughts on this report? Let us know!

Acid-Override pushed a commit to Acid-Override/mcp-email-server that referenced this pull request Feb 18, 2026
… (search optimization + filter validation)

Combined optimizations:
- PR ai-zerolab#116: Add list_mailboxes, move_emails, mark_emails_as_read, search_emails tools
- PR ai-zerolab#116: UID-based pagination optimization (60s+ → <5s on large mailboxes)
- PR ai-zerolab#117: Filter validation (prevents accidental expensive searches)
- PR ai-zerolab#117: Search result caching (_last_search_total)

Conflict resolution: Merged parse_search_response logic with caching to avoid duplicate searches.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant